package instructions;

import values.Function;
import base.Environment;
import base.Identifier;

public class FunctionDef implements Instruction {

	private Identifier name;
	private Instruction body;
	private Identifier args[];
	
	/**
	 * creates the definition of a function
	 */
	public FunctionDef (Identifier name, Instruction body,  Identifier args[]) {
		this.name = name;
		this.body = body;
		this.args = args;
	}
	
	/**
	 *  Adds the function, its body and its contextual environment to the environment
	 */
	public Environment instr(Environment env) {
		
		Environment context = env.clone();
		
		Function fun = new Function(this.body, context, this.args);
		context.update(this.name, fun);
		
		Environment newEnv = env.clone();
		newEnv.update(this.name, fun);
		
		return newEnv;
	}
	
}
